import logging
from datetime import datetime,timedelta,UTC
import os
import  sys
from core.config import settings
from core.exceptions import ApiException
from  typing import Dict,Any,Optional
from fastapi import Header

logger = logging.getLogger(__name__)

def remove_prefix(token)->str:
    return token.replace(settings.JWT_LOGIN_SUBJECT, "")


async def get_currnt_user(token:Optional[str]=Header(None))->Dict[str,Any]:
    try:
        if not token:
            raise ApiException(status_code=401,detail="未登录") 
        token = remove_prefix(token)
    
        payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms=[settings.JWT_ALGORITHM]) 
    
        logger.info(payload) 
    
        return{"username":payload["username"],"account_id":payload["accountId"]} 
    except Exception as e:
        logger.error(e)
